package com.simple.portal.dao.impl;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.simple.base.dao.BaseDAO;
import com.simple.portal.dao.IGroupInfoDAO;
import com.simple.portal.vo.GroupInfoVO;
import com.simple.util.common.Constants;
import com.simple.util.common.StringUtil;
import com.simple.util.object.PageInfoBean;

@Repository("groupInfoDAO")
public class GroupInfoDAOImpl extends BaseDAO implements IGroupInfoDAO {

	@Override
	public GroupInfoVO findById(final String id) {
		
		return (GroupInfoVO)super.get(GroupInfoVO.class, id);
	}
	
	@Override
	public GroupInfoVO save(final GroupInfoVO groupInfoVO,final String operator) {
		
		GroupInfoVO tempGroupInfoVO=super.save(groupInfoVO);
		super.saveLogger(operator, tempGroupInfoVO, tempGroupInfoVO.getGroupId(),Constants.LOGGER_OPERATE_FLAG_ADD);
		return tempGroupInfoVO;
	
	}
	
	@Override
	public void update(final GroupInfoVO groupInfoVO,final String type,final String operator) {
		
		super.update(groupInfoVO);
		super.saveLogger(operator, groupInfoVO, groupInfoVO.getGroupId(),type);
	}

	@Override
	public List<GroupInfoVO> findAll() {
		
		return super.query(GroupInfoVO.class);
	}

	@Override
	public void delete(final GroupInfoVO groupInfoVO,final String operator) {
		
		super.delete(groupInfoVO);
		super.saveLogger(operator, groupInfoVO, groupInfoVO.getGroupId(), Constants.LOGGER_OPERATE_FLAG_DELETE);
	}

	@Override
	public void delete(final String id,final String operator) {
		GroupInfoVO groupInfoVO= findById(id);
		super.delete(GroupInfoVO.class, id);
		super.saveLogger(operator, groupInfoVO, groupInfoVO.getGroupId(), Constants.LOGGER_OPERATE_FLAG_DELETE);
	}
	

	@Override
	public List<?> findGroupInfoByPage(final String userId,final String groupName,final String organizationId,final  String status,final PageInfoBean pageInfoBean){
		final StringBuffer sb = new StringBuffer(300);
		final Map<String, Object> paramMap = new LinkedHashMap<String, Object>();

		sb.append("select g,org from GroupInfoVO g,OrganizationVO org  where g.organizationId = org.organizationId ");
		sb.append(" and g.creator =:userId ");
		paramMap.put("userId", userId);

		if (!StringUtil.isNull(groupName)) {
			sb.append(" and g.groupName like :groupName");
			paramMap.put("groupName", "%" + groupName + "%");
		}
		if (!StringUtil.isNull(organizationId)) {
			sb.append(" and org.organizationId=:organizationId");
			paramMap.put("organizationId", organizationId);
		}
		if (!StringUtil.isNull(status)) {
			sb.append(" and g.status=:status");
			paramMap.put("status", status);
		}

		sb.append(" order by g.createDate desc");

		return super.queryListByPage("g.groupId", sb.toString(), paramMap, pageInfoBean);
		
	}

	@Override
	public List<GroupInfoVO> findByCreator(String userId) {
		final StringBuffer sb = new StringBuffer(300);
		sb.append("select g from GroupInfoVO g where g.creator = ?");
		return super.query(sb.toString(), userId);
	}



}
